package com.example.newsapp.ui.screens.mainScreens

import android.Manifest
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newsapp.R
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberPermissionState

// TODO 天气界面
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
@Composable
fun WeatherScreen(modifier: Modifier = Modifier) {
    val backgroundImage = painterResource(id = R.drawable.weather_background)
    var permission = rememberPermissionState(
        permission = Manifest.permission.ACCESS_FINE_LOCATION
    )
    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(
                        text = "合肥",
                        textAlign = TextAlign.Center,
                        fontWeight = FontWeight.Bold, fontSize = 25.sp,
                        modifier = Modifier.fillMaxWidth()
                    )
                },
                colors = TopAppBarDefaults.topAppBarColors(containerColor = Color.Transparent),
            )
        }
    ) { paddingValues ->
        LazyColumn {
            item {
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.Transparent)
                ) {
                    Image(
                        painter = backgroundImage,
                        contentDescription = null,
                        modifier = Modifier.fillMaxSize(),
                        contentScale = ContentScale.Crop
                    )
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(paddingValues)
                            .padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        // 当前温度和天气状况
                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 16.dp),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Icon(
                                painter = painterResource(R.drawable.baseline_cloud_24),
                                contentDescription = null,
                                tint = Color.White,
                                modifier = Modifier.size(32.dp)
                            )
                            Column(
                                horizontalAlignment = Alignment.End
                            ) {
                                Text(
                                    text = "22℃",
                                    style = TextStyle(
                                        fontSize = 50.sp,
                                        fontWeight = FontWeight.Bold,
                                        color = Color.White
                                    )
                                )
                                Text(
                                    text = "多云",
                                    style = TextStyle(
                                        fontSize = 18.sp,
                                        color = Color.White
                                    )
                                )
                            }
                        }
                        // 天气预报
                        Card(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 16.dp)
                                .clip(RoundedCornerShape(8.dp)),
                            colors = CardDefaults.cardColors(containerColor = Color.Black.copy(alpha = 0.5f)),
                            elevation = CardDefaults.elevatedCardElevation(defaultElevation = 8.dp)
                        ) {
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(16.dp)
                            ) {
                                Text(
                                    text = "预报",
                                    style = TextStyle(
                                        fontSize = 20.sp,
                                        fontWeight = FontWeight.Bold,
                                        color = Color.White
                                    )
                                )
                                Column {
                                    repeat(6) { index ->
                                        val date = when (index) {
                                            0 -> "2025-05-01"
                                            1 -> "2025-05-02"
                                            2 -> "2023-05-03"
                                            3 -> "2025-05-04"
                                            4 -> "2025-05-05"
                                            5 -> "2025-05-06"
                                            else -> "2025-05-07"
                                        }
                                        val weather = when (index) {
                                            0, 1, 2 -> "多云"
                                            3 -> "晴"
                                            4, 5 -> "阴"
                                            else -> "阴"
                                        }
                                        val maxTemp = when (index) {
                                            0 -> 29
                                            1 -> 27
                                            2 -> 26
                                            3 -> 28
                                            4 -> 29
                                            5 -> 25
                                            else -> 29
                                        }
                                        val minTemp = when (index) {
                                            0 -> 16
                                            1 -> 16
                                            2 -> 19
                                            3 -> 18
                                            4 -> 20
                                            5 -> 12
                                            else -> 19
                                        }
                                        Row(
                                            modifier = Modifier
                                                .fillMaxWidth()
                                                .padding(vertical = 4.dp),
                                            horizontalArrangement = Arrangement.SpaceBetween,
                                            verticalAlignment = Alignment.CenterVertically
                                        ) {
                                            Text(
                                                text = date,
                                                style = TextStyle(
                                                    fontSize = 16.sp,
                                                    color = Color.White
                                                )
                                            )
                                            Text(
                                                text = weather,
                                                style = TextStyle(
                                                    fontSize = 16.sp,
                                                    color = Color.White
                                                )
                                            )
                                            Text(
                                                text = "$maxTemp",
                                                style = TextStyle(
                                                    fontSize = 16.sp,
                                                    color = Color.White
                                                )
                                            )
                                            Text(
                                                text = "$minTemp",
                                                style = TextStyle(
                                                    fontSize = 16.sp,
                                                    color = Color.White
                                                )
                                            )
                                        }
                                    }
                                }
                            }
                        }
                        // 空气质量
                        Card(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(bottom = 16.dp)
                                .clip(RoundedCornerShape(8.dp)),
                            colors = CardDefaults.cardColors(containerColor = Color.Black.copy(alpha = 0.5f)),
                            elevation = CardDefaults.elevatedCardElevation(defaultElevation = 4.dp)
                        ) {
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(16.dp)
                            ) {
                                Text(
                                    text = "空气质量",
                                    style = TextStyle(
                                        fontSize = 20.sp,
                                        fontWeight = FontWeight.Bold,
                                        color = Color.White
                                    )
                                )
                                Row(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .padding(vertical = 4.dp),
                                    horizontalArrangement = Arrangement.SpaceBetween,
                                    verticalAlignment = Alignment.CenterVertically
                                ) {
                                    Column {
                                        Text(
                                            text = "101",
                                            style = TextStyle(
                                                fontSize = 32.sp,
                                                fontWeight = FontWeight.Bold,
                                                color = Color.White
                                            )
                                        )
                                        Text(
                                            text = "AQI指数",
                                            style = TextStyle(
                                                fontSize = 16.sp,
                                                color = Color.White
                                            )
                                        )
                                    }
                                    Column {
                                        Text(
                                            text = "41",
                                            style = TextStyle(
                                                fontSize = 32.sp,
                                                fontWeight = FontWeight.Bold,
                                                color = Color.White
                                            )
                                        )
                                        Text(
                                            text = "PM2.5指数",
                                            style = TextStyle(
                                                fontSize = 16.sp,
                                                color = Color.White
                                            )
                                        )
                                    }
                                }
                            }
                        }
                        // 生活建议
                        Card(
                            modifier = Modifier
                                .fillMaxWidth()
                                .clip(RoundedCornerShape(8.dp)),
                            colors = CardDefaults.cardColors(containerColor = Color.Black.copy(alpha = 0.5f)),
                            elevation = CardDefaults.elevatedCardElevation(defaultElevation = 4.dp)
                        ) {
                            Column(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(16.dp)
                            ) {
                                Text(
                                    text = "生活建议",
                                    style = TextStyle(
                                        fontSize = 20.sp,
                                        fontWeight = FontWeight.Bold,
                                        color = Color.White
                                    )
                                )
                                Text(
                                    text = "舒适度: 白天不太热也不太冷，风力不大，相信您在这样的天气条件下，应会感到比较清爽和舒适。",
                                    style = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.White
                                    ),
                                    textAlign = TextAlign.Justify
                                )
                                Text(
                                    text = "洗车指数: 较适宜洗车，未来一天无雨，风力较小，擦洗一新的汽车至少能保持一天。",
                                    style = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.White
                                    ),
                                    textAlign = TextAlign.Justify
                                )
                                Text(
                                    text = "运行建议: 天气较好，但因风力稍强，户外可选择对风力要求不高的运动，推荐您进行室内运动。",
                                    style = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.White
                                    ),
                                    textAlign = TextAlign.Justify
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}